#include "TestCommon.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "FileSystem/FileSystem.h"

BOOST_AUTO_TEST_SUITE(TestFileSystem)

BOOST_AUTO_TEST_CASE(TestFileSystemMain)
{
	FileSystem_Init();
	FileHandle file;

	/* check Open function */
	BOOST_REQUIRE(false == Open(&file, "/this/is/not/exists/file/hahaha", FM_OpenRead));

	unlink("./newfile");
	BOOST_REQUIRE(false == Open(&file, "./newfile", FM_OpenRead));
	BOOST_REQUIRE(true == Open(&file, "./newfile", FM_OpenRW | FM_Create));

	/* check Write function */
	BOOST_REQUIRE(6 == Write(&file, "preved", 6));
	BOOST_REQUIRE(1 == Write(&file, " ", 1));
	BOOST_REQUIRE(6 == Write(&file, "medved", 6));

	/* check Seek & Read function */
	BOOST_REQUIRE(0 == Seek(&file, 0, SEEK_SET));
	char buf[256];
	
	memset(buf, 0, sizeof(buf));
	BOOST_REQUIRE(13 == Read(&file, buf, sizeof(buf)));
	BOOST_REQUIRE(0 == strcmp("preved medved", buf));
	BOOST_REQUIRE(7 == Seek(&file, 7, SEEK_SET));
	
	memset(buf, 0, sizeof(buf));
	BOOST_REQUIRE(6 == Read(&file, buf, sizeof(buf)));

	/* check PRead && Seek */
	BOOST_REQUIRE(0 == strcmp("medved", buf));
	BOOST_REQUIRE(0 == Seek(&file, 0, SEEK_SET));
	
	memset(buf, 0, sizeof(buf));
	BOOST_REQUIRE(6 == PRead(&file, buf, sizeof(buf), 7));
	BOOST_REQUIRE(0 == strcmp("medved", buf));
	
	memset(buf, 0, sizeof(buf));
	BOOST_REQUIRE(13 == Read(&file, buf, sizeof(buf)));
	BOOST_REQUIRE(0 == strcmp("preved medved", buf));
	
	/* check PWrite function */
	BOOST_REQUIRE(6 == PWrite(&file, "medved", 6, 0));
	BOOST_REQUIRE(6 == PWrite(&file, "preved", 6, 7));


	BOOST_REQUIRE(0 == Seek(&file, 0, SEEK_SET));
	memset(buf, 0, sizeof(buf));
	BOOST_REQUIRE(13 == Read(&file, buf, sizeof(buf)));
	BOOST_REQUIRE(0 == strcmp("medved preved", buf));

	FileSystem_Free();
};

BOOST_AUTO_TEST_SUITE_END()

